/********DS3231基础定义*********/
//文件名：DS3231.C
//时间：2022/01/16
//作者：吴东桓
//晶振：11.0954mhz
//STC12C
#include "DS3231.h"
//sbit SDA = P2^6;     //模拟I2C数据传送位SDA        
//sbit SCL = P2^7;     //模拟I2C时钟控制位SCL
//
u8 BCD2HEX(u8 val)    //BCD转换为Byte
{
     u8 temp;
     temp=val&0x0f;
     val>>=4;
     val&=0x0f;
     val*=10;
     temp+=val;
     return temp;
}

u8 HEX2BCD(u8 val)    //B码转换为BCD码
{
     u8 i,j,k;
     i=val/10;
     j=val%10;
     k=j+(i<<4);
     return k;
}

void write_DS3231_byte(u8 addr, u8 write_data)
{
     I2C_Start_time();
     Write_IIC_Byte_time(DS3231_WriteAddress);
     Write_IIC_Byte_time(addr);   
     Write_IIC_Byte_time(write_data);
     I2C_Stop_time();
     Delay10us();      
}

u8 read_DS3231_current()
{
     u8 read_data;
     I2C_Start_time();
     Write_IIC_Byte_time(DS3231_ReadAddress);
     read_data = Read_IIC_Byte_time();
     Send_NOT_ACK_time();
     I2C_Stop_time();
     return read_data;
}

u8 read_DS3231_random(u8 random_addr)
{
     I2C_Start_time();
     Write_IIC_Byte_time(DS3231_WriteAddress);
     Write_IIC_Byte_time(random_addr);
     return(read_DS3231_current());
}

void ModifyTime(u8 yea,u8 mon,u8 da,u8 hou,u8 min,u8 sec)//修改年月日，时分秒
{
     u8 temp=0;
     temp=HEX2BCD(yea);
     write_DS3231_byte(DS3231_YEAR,temp);   //修改年
     temp=HEX2BCD(mon);
     write_DS3231_byte(DS3231_MONTH,temp);  //修改月
     temp=HEX2BCD(da);
     write_DS3231_byte(DS3231_DAY,temp);    //修改日
     temp=HEX2BCD(hou);
     write_DS3231_byte(DS3231_HOUR,temp);   //修改时
     temp=HEX2BCD(min);
     write_DS3231_byte(DS3231_MINUTE,temp); //修改分
     temp=HEX2BCD(sec);
     write_DS3231_byte(DS3231_SECOND,temp); //修改秒
	
}

